package com.junlong0716.enjoylearning.ui

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.widget.LinearLayoutManager
import com.junlong0716.enjoylearning.R
import com.junlong0716.enjoylearning.adapter.FindMultipleItemAdapter
import com.junlong0716.enjoylearning.constant.NetConstant
import com.junlong0716.enjoylearning.entity.FindCommendTopicEntity
import com.junlong0716.enjoylearning.net.api.NetApi
import com.junlong0716.getknowledgedemo.decoration.DividerItemDecoration
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_topic_list.*
import kotlinx.android.synthetic.main.view_title_bar.*
import org.json.JSONObject

class TopicListActivity : AppCompatActivity() {
    private lateinit var commendTopicData: ArrayList<FindCommendTopicEntity>
    private lateinit var commendTopicAdapter: FindMultipleItemAdapter.CommendTopicAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_topic_list)
        rl_back.setOnClickListener { finish() }
        tv_title.text = "全部专题"
        commendTopicData = ArrayList()
        rv_topic_list.layoutManager = LinearLayoutManager(this)
        commendTopicAdapter = FindMultipleItemAdapter.CommendTopicAdapter(commendTopicData, R.layout.child_item_find_commend_topic_layout)
        rv_topic_list.adapter = commendTopicAdapter

        rv_topic_list.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST, 3, ContextCompat.getColor(this, R.color.divider_gray)))

        srl_refresh.autoRefresh()

        srl_refresh.setOnRefreshListener {
            requestAllTopic()
        }

        srl_refresh.isEnableLoadMore = false

        commendTopicAdapter.setOnItemClickListener { _, _, position ->
            val topicIntent = Intent(this, TopicActivity::class.java)
            topicIntent.putExtra("TOPIC_ID", commendTopicData[position].topicId)
            topicIntent.putExtra("TOPIC_NAME", commendTopicData[position].topicTitle)
            topicIntent.putExtra("TOPIC_IMAGE", commendTopicData[position].imageUrl)
            topicIntent.putExtra("TOPIC_DESCRIBE", commendTopicData[position].topicDescription)
            topicIntent.putExtra("TOPIC_IS_PRAISE", commendTopicData[position].topicIsPraise)
            topicIntent.putExtra("TOPIC_PRAISE_NUM", commendTopicData[position].topicPraiseNum)
            this.startActivity(topicIntent)
        }

    }

    private fun requestAllTopic() {

        OkGo.post<String>(NetConstant.REQUEST_HEADER + NetApi.API_TOPIC_QUERY)
                .tag(this)
                .upJson(JSONObject())
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        val jsonObject = JSONObject(response!!.body())
                        val code = jsonObject.getString("code")
                        if (code == "200") {
                            srl_refresh.finishRefresh()
                            commendTopicData.clear()
                            val data = jsonObject.getJSONObject("data")
                            val topicArray = data.getJSONArray("list")
                            for (i in 0 until topicArray.length()) {
                                val id = topicArray.getJSONObject(i).getInt("id")
                                val specialTitle = topicArray.getJSONObject(i).getString("specialTitle")
                                val coverImg = topicArray.getJSONObject(i).getString("coverImg")
                                val createDate = topicArray.getJSONObject(i).getLong("createDate")
                                val knowledgeDescribe = topicArray.getJSONObject(i).getString("knowledgeDescribe")
                                val topicIsPraise = topicArray.getJSONObject(i).getInt("isPraise")


                                val topicPraiseNum = try {
                                    topicArray.getJSONObject(i).getInt("praiseNumber")
                                } catch (e: Exception) {
                                    0
                                }
                                commendTopicData.add(FindCommendTopicEntity(id, coverImg, specialTitle, knowledgeDescribe, createDate, topicIsPraise, topicPraiseNum))
                            }
                            commendTopicAdapter.notifyDataSetChanged()
                        }
                    }
                })
    }
}
